// ==UserScript==
// @name         使用当前页面Cookie发送请求示例
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  演示油猴脚本如何使用当前页面的Cookie发送请求
// @author       你的名字
// @match        https://example.com/*  // 匹配需要运行脚本的网站
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  // 新增方法：获取资源下载链接
  function getResourceDownloadUrl() {
    const url =
      "https://www.doubao.com/samantha/code/get_resource_download_url";

    const headers = {
      accept: "application/json, text/plain, */*",
      "accept-language": "zh-CN,zh;q=0.9",
      "agw-js-conv": "str",
      "content-type": "application/json",
      origin: "https://www.doubao.com",
      priority: "u=1, i",
      referer: window.location.href,
      "sec-ch-ua":
        '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"',
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": '"Windows"',
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-origin",
      "user-agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36",
    };

    const body = {
      type: 3,
      artifacts_identifier: {
        code_id: "20378744161911554",
        code_version: 7,
        title: "wabi-sabi-startpage-3x3",
      },
    };

    fetch(url, {
      method: "POST",
      headers: headers,
      credentials: "include", // 携带当前页面的Cookie
      body: JSON.stringify(body),
    })
      .then((response) => response.json())
      .then((data) => console.log("获取下载链接结果:", data))
      .catch((error) => console.error("获取下载链接出错:", error));
  }
  if (
    document.readyState === "complete" ||
    document.readyState === "interactive"
  ) {
    // 调用新方法
    getResourceDownloadUrl();
  }
})();
